package se.webbzon.boltzmann.game.object.spawner;

import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.boltzmann.game.character.CharacterException;
import se.webbzon.boltzmann.game.object.dialogue.DialogueVoice;
import se.webbzon.boltzmann.game.object.npc.FriendlyAI;
import se.webbzon.boltzmann.game.object.npc.FriendlyNPC;
import se.webbzon.boltzmann.npc.NPCDuty;
import se.webbzon.boltzmann.object.unknown.Unknown;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class FriendlyNPCSpawnerDeflation extends AbstractNPCSpawnerDeflation {

	private FriendlyAI ai;
	
	private DialogueVoice voice;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new empty friendly non-player character spawner 
	 * deflation. **/
	public FriendlyNPCSpawnerDeflation(	int deflationProperties) {
		super(deflationProperties);
	}
	
	/** Creates a new friendly non-player character spawner deflation from
	 * an existing spawner. **/
	public FriendlyNPCSpawnerDeflation(	int deflationProperties,
										FriendlyNPCSpawner spawner) {
		super(deflationProperties, spawner);
		ai = spawner.getAI();
		voice = spawner.getVoice();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		ai = FriendlyAI.values()[buff.getInt()];
		voice = DialogueVoice.values()[buff.getInt()];
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff =  super.deflate(allocate + 8);
		buff.putInt(ai.ordinal());
		buff.putInt(voice.ordinal());
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	/** Creates a new spawner or non-player character depending on the
	 * type of the provided environment. **/
	@Friendly WorldObject createSpawnerOrNPC(AbstractEnvironment env) {
		final double dy;
		WorldStaticObject instance;
		NPCDuty d = null;
		
		if (!spawn(env)) {
			instance = 
					new FriendlyNPCSpawner(	getId(), getKey(),
											getCharacterTextureSetName(),
											ai, voice);
			instance.setVisible(env.isDebugMode());
			dy = 0;
		} else try {
			FriendlyNPC npc = new FriendlyNPC(getCharacterTextureSetName(),ai,voice);
			d = npc.getDuty();
			instance = npc;
		} catch (CharacterException e) {
			instance = new Unknown();
		} finally {
			dy = 0.5;
		}
		
		instance.setPosition(getX(),getY() + dy,getZ());
		instance.setSize(getWidth(), getHeight(), getDepth());
		instance.setRotation(getEX(), getEY(), getEZ(), getAngle());
		if (d != null)
			d.setOrigin(getX(), getY(), getZ());
		
		return instance;
	}
	

}
